package org.mtdownload.dialogs.preferences;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.mtdownload.utils.PreferencesManager;

public class ProgressColors extends PreferencePanel {

	private static final long serialVersionUID = 3618983377688999476L;

	private JList colorsList=new JList();
	private DefaultListModel colorsListModel=new DefaultListModel();
	private JComboBox singleColorCombo=new JComboBox();
	private JComboBox backgroundColorCombo=new JComboBox();
	private JComboBox textColorCombo=new JComboBox();
	
	public ProgressColors() {
		super("Progress colors");
		init();
	}
	
	private void init(){
		setLayout(new GridBagLayout());
		JPanel colorsListPanel=new JPanel(new GridBagLayout());
		colorsListPanel.setBorder(BorderFactory.createTitledBorder("Download graph list of colors"));
		colorsList.setModel(colorsListModel);
		JScrollPane colorsScroll=new JScrollPane(colorsList);
		
		JPanel colorListControlButtons=new JPanel();		
		colorListControlButtons.setLayout(new BoxLayout(colorListControlButtons,BoxLayout.PAGE_AXIS));
		JButton addColorButton=new JButton("Add");
		JButton editColorButton=new JButton("Edit");
		JButton delColorButton=new JButton("Del");
		JButton upColorButton=new JButton("Up");
		JButton downColorButton=new JButton("Down");
		addColorButton.setAlignmentX(Component.CENTER_ALIGNMENT);
		editColorButton.setAlignmentX(Component.CENTER_ALIGNMENT);
		delColorButton.setAlignmentX(Component.CENTER_ALIGNMENT);
		upColorButton.setAlignmentX(Component.CENTER_ALIGNMENT);
		downColorButton.setAlignmentX(Component.CENTER_ALIGNMENT);
		colorListControlButtons.add(Box.createVerticalGlue());
		colorListControlButtons.add(addColorButton);
		colorListControlButtons.add(editColorButton);
		colorListControlButtons.add(delColorButton);
		colorListControlButtons.add(upColorButton);
		colorListControlButtons.add(downColorButton);
		colorListControlButtons.add(Box.createVerticalGlue());
		addColorButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				Color c=JColorChooser.showDialog(ProgressColors.this,"Add color",null);
				if(c!=null){
					colorsListModel.addElement(c);
					getPreferencesListener().preferencesChanged();
				}
			}
		});
		editColorButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				int index=colorsList.getSelectedIndex();
				if(index<0)return;
				Color c=JColorChooser.showDialog(ProgressColors.this,"Edit color",(Color)colorsListModel.get(index));
				if(c!=null){
					colorsListModel.set(index,c);
					getPreferencesListener().preferencesChanged();
				}
			}
		});
		delColorButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				int index=colorsList.getSelectedIndex();
				if(index<0)return;
				colorsListModel.remove(index);
				getPreferencesListener().preferencesChanged();
			}
		});
		upColorButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				int index=colorsList.getSelectedIndex();
				if(index<=0)return;
				Color c1=(Color)colorsListModel.get(index-1);
				colorsListModel.set(index-1,colorsListModel.get(index));
				colorsListModel.set(index,c1);
				colorsList.setSelectedIndex(index-1);
				colorsList.ensureIndexIsVisible(index-1);
				getPreferencesListener().preferencesChanged();
			}
		});
		downColorButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				int index=colorsList.getSelectedIndex();
				if(index<0||index>colorsListModel.size()-2)return;
				Color c1=(Color)colorsListModel.get(index+1);
				colorsListModel.set(index+1,colorsListModel.get(index));
				colorsListModel.set(index,c1);
				colorsList.setSelectedIndex(index+1);
				colorsList.ensureIndexIsVisible(index+1);
				getPreferencesListener().preferencesChanged();
			}
		});
		List<Color> colors=PreferencesManager.getProgressColorsList();
		for(int i=0;i<colors.size();i++){
			colorsListModel.addElement(colors.get(i));
		}
		colorsList.setCellRenderer(new ColorsListCellRenderer());
		colorsListPanel.add(colorsScroll,new GridBagConstraints(0,0,1,1,0.5,1,GridBagConstraints.WEST,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0));
		colorsListPanel.add(colorListControlButtons,new GridBagConstraints(1,0,1,1,0,1,GridBagConstraints.WEST,GridBagConstraints.VERTICAL,new Insets(0,0,0,0),0,0));		
		JLabel helpLabel=new JLabel("<html><body>The colors specified in this list are the colors"+
										" used when the multi-color download graph is displayed. Each color"+
										" will be chosen for one thread. If there are more threads than "+
										"available colors then the first one will be chosen again.</body></html>");
		helpLabel.setPreferredSize(new Dimension(200,200));
		colorsListPanel.add(helpLabel,new GridBagConstraints(2,0,1,1,0.5,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0));
						
		JPanel singleColorPanel=setupProgressColorPanel(singleColorCombo,"Progress graph single color",PreferencesManager.getProgressSingleColor());
		JPanel backgroundColorPanel=setupProgressColorPanel(backgroundColorCombo,"Progress graph background color",PreferencesManager.getProgressBackgroundColor());
		JPanel textColorPanel=setupProgressColorPanel(textColorCombo,"Progress graph text color",PreferencesManager.getProgressTextColor());
		add(colorsListPanel,new GridBagConstraints(0,0,1,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0));
		add(singleColorPanel,new GridBagConstraints(0,1,1,1,1,0,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0));
		add(backgroundColorPanel,new GridBagConstraints(0,2,1,1,1,0,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0));
		add(textColorPanel,new GridBagConstraints(0,3,1,1,1,0,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0));
	}
	
	private JPanel setupProgressColorPanel(JComboBox combo,String title,final Color defaultColor){
		JPanel p=new JPanel();
		p.setBorder(BorderFactory.createTitledBorder(title));
		combo.setEditable(false);
		combo.setRenderer(new ColorsListCellRenderer());
		final DefaultComboBoxModel model=new DefaultComboBoxModel();
		combo.setModel(model);
		addComboItems(model);		
		if(model.getIndexOf(defaultColor)>=0){
			model.setSelectedItem(defaultColor);
		}else{
			model.addElement(defaultColor);
			model.setSelectedItem(defaultColor);			
		}
		
		JButton button=new JButton("Add custom");
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				Color c=JColorChooser.showDialog(ProgressColors.this,"Choose color",defaultColor);
			  	if(c==null) return;
				model.addElement(c);
				model.setSelectedItem(c);				
			}
		});
		JButton rbutton=new JButton("Reset");
		rbutton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				Color c=defaultColor;
			  	if(c==null) return;
				if(model.getIndexOf(c)>=0){
					model.setSelectedItem(c);
				}else{
					model.addElement(c);
					model.setSelectedItem(c);			
				}
			}
		});
		
		p.add(combo);
		p.add(button);	
		p.add(rbutton);
		combo.addItemListener(new ItemListener(){
			public void itemStateChanged(ItemEvent e) {
				if(e.getStateChange()==ItemEvent.SELECTED){
					getPreferencesListener().preferencesChanged();
				}
			}
		});	
		return p;
	}

	private void addComboItems(DefaultComboBoxModel model){
		model.addElement(Color.white);
		model.addElement(Color.lightGray);
		model.addElement(Color.gray);
		model.addElement(Color.darkGray);
		model.addElement(Color.black);
		model.addElement(Color.red);
		model.addElement(Color.pink);
		model.addElement(Color.orange);
		model.addElement(Color.yellow);
		model.addElement(Color.green);
		model.addElement(Color.magenta);
		model.addElement(Color.cyan);
		model.addElement(Color.blue);
	}
	
	@Override
	public void applyChanges() {
		List<Color> colors=new ArrayList<Color>();
		for(int i=0;i<colorsListModel.size();i++){
			colors.add((Color)colorsListModel.elementAt(i));
		}
		PreferencesManager.setProgressColorsList(colors);
		PreferencesManager.setProgressSingleColor((Color)singleColorCombo.getSelectedItem());
		PreferencesManager.setProgressBackgroundColor((Color)backgroundColorCombo.getSelectedItem());
		PreferencesManager.setProgressTextColor((Color)textColorCombo.getSelectedItem());
	}

}
